package com.health;

import java.text.SimpleDateFormat;
import java.util.List;

import com.health.db.FDbManager;
import com.health.model.BD_DD_INFO;
import com.health.tablebean.ARCHIVE_ASSISTCHECK;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_CUSTOMERBASEINFO;
import com.health.tablebean.ARCHIVE_GENERALCONDITION;
import com.health.tablebean.SBCL_DATA;
import com.health.tools.DataCalc;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
/**
 * 综合报告
 * @author nova
 *
 */
public class ShowJieLunPicActivity extends Activity {
	private static final String TAG = "ShowJieLunPicActivity";

	private Button btn_back;
	private Button btn_up;

	private TextView title;
	private FDbManager fdbmanger = null;
	private ARCHIVE_BASEINFO userInfo;// 基本信息
	String IDCARD;

	TextView name;// 姓名
	TextView idcard;// 身份证号
	TextView sex;// 性别
	TextView birthday;// 出生日期
	TextView phone;// 联系电话
	TextView address;// 家庭住址
	TextView date;// 体检时间
	TextView tizhong;// 体重
	TextView xueya;// 右侧血压
	TextView xueyazuo;// 左侧血压
	TextView shengao;// 身高
	TextView tizhizhishu;// 体质指数
	TextView yaowei;// 腰围
	TextView tiwen;// 体温
	TextView huxipinlv;// 呼吸频率
	TextView mailv;// 脉率
	TextView xuetang;// 血糖
	TextView xueyang;// 血氧
	TextView xindian;// 心电
	TextView xindianP;// 心电评论
	TextView PRO;// 尿蛋白
	TextView KET;// 尿酮体
	TextView GLU;// 尿糖
	TextView BLD;// 尿潜血
	TextView URO;// 尿胆原
	TextView BIL;// 胆红素
	TextView LEU;// 白细胞
	TextView VC;// 维生素C
	TextView SG;// 比重
	TextView NIT;// 硝酸盐
	TextView PH;// PH值
	TextView U_MA;// 微量白蛋白
	TextView jielun;// 结论
	TextView jianyi;// 建议
	
	TextView xiongtou;//胸透
	TextView b_chao;//B超
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_show_jielun_pic);
		initView();
		setData();
		setListens();
	}

	private void initView() {

		btn_back = (Button) findViewById(R.id.btn_back);
		btn_up = (Button) findViewById(R.id.btn_up);
		btn_up.setVisibility(View.GONE);

		title = (TextView) findViewById(R.id.title);
		title.setText("体检报告");

		name = (TextView) findViewById(R.id.name);
		idcard = (TextView) findViewById(R.id.idcard);
		sex = (TextView) findViewById(R.id.sex);
		birthday = (TextView) findViewById(R.id.birthday);
		phone = (TextView) findViewById(R.id.phone);
		address = (TextView) findViewById(R.id.address);
		date = (TextView) findViewById(R.id.date);
		tizhong = (TextView) findViewById(R.id.tizhong);
		xueya = (TextView) findViewById(R.id.xueya);
		xueyazuo = (TextView) findViewById(R.id.xueyazuo);
		shengao = (TextView) findViewById(R.id.shengao);
		tizhizhishu = (TextView) findViewById(R.id.tizhizhishu);
		yaowei = (TextView) findViewById(R.id.yaowei);
		tiwen = (TextView) findViewById(R.id.tiwen);
		huxipinlv = (TextView) findViewById(R.id.huxipinlv);
		mailv = (TextView) findViewById(R.id.mailv);
		xuetang = (TextView) findViewById(R.id.xuetang);
		xueyang = (TextView) findViewById(R.id.xueyang);
		xindian = (TextView) findViewById(R.id.xindian);
		xindianP = (TextView) findViewById(R.id.xindianP);
		PRO = (TextView) findViewById(R.id.PRO);
		KET = (TextView) findViewById(R.id.KET);
		GLU = (TextView) findViewById(R.id.GLU);
		BLD = (TextView) findViewById(R.id.BLD);
		URO = (TextView) findViewById(R.id.URO);
		BIL = (TextView) findViewById(R.id.BIL);
		LEU = (TextView) findViewById(R.id.LEU);
		VC = (TextView) findViewById(R.id.VC);
		SG = (TextView) findViewById(R.id.SG);
		NIT = (TextView) findViewById(R.id.NIT);
		PH = (TextView) findViewById(R.id.PH);
		U_MA = (TextView) findViewById(R.id.MA);
		jielun = (TextView) findViewById(R.id.jielun);
		jianyi = (TextView) findViewById(R.id.jianyi);
		 xiongtou= (TextView) findViewById(R.id.xiongtou);
		 b_chao= (TextView) findViewById(R.id.b_chao);
	}

	public void setData() {
		String jiLunResult = "";
		String JianYiResult = "";
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		if (userInfo != null) {
			IDCARD = NovaApplication.getUserInfo().getIDCARD();
			// 一般情况
			List<ARCHIVE_GENERALCONDITION> ags = fdbmanger.findAllByWhere(ARCHIVE_GENERALCONDITION.class,
					"IDCARD = '" + userInfo.getIDCARD() + "'");
			List<ARCHIVE_CUSTOMERBASEINFO> acs = fdbmanger.findAllByWhere(ARCHIVE_CUSTOMERBASEINFO.class,
					"IDCARD = '" + userInfo.getIDCARD() + "'");
			// 辅助检查
			List<ARCHIVE_ASSISTCHECK> assistChecks = fdbmanger.findAllByWhere(ARCHIVE_ASSISTCHECK.class,
					"IDCARD = '" + userInfo.getIDCARD() + "'");
			name.setText(userInfo.getCUSTOMERNAME());
			idcard.setText(userInfo.getIDCARD());
			if ("DM01-08_2".equals(userInfo.getSEX())) {
				sex.setText("女");
			} else {
				sex.setText("男");
			}

			birthday.setText(formatter.format(userInfo.getBIRTHDAY()));
			phone.setText(userInfo.getPHONE());
//			address.setText(userInfo.getADDR());
			address.setText(userInfo.getHOUSEHOLD_ADDR());
			if (ags.size() > 0) {
				tizhong.setText(ags.get(0).getWEIGHT() + " kg");
				shengao.setText(ags.get(0).getHEIGHT() + " cm");
				yaowei.setText(ags.get(0).getWAISTLINE() + " cm");
				huxipinlv.setText(ags.get(0).getBREATH_RATE() + " 次/分");
				if (!TextUtils.isEmpty(ags.get(0).getRIGHTHEIGHT())) {
					xueya.setText(ags.get(0).getRIGHTHEIGHT() + "/" + ags.get(0).getRIGHTPRE() + " mmHg");
					if (!DataCalc.calcXueYa(ags.get(0).getRIGHTHEIGHT(), ags.get(0).getRIGHTPRE())) {
						jiLunResult = jiLunResult+"右侧测量"
								+ DataCalc.getXueYa(ags.get(0).getRIGHTHEIGHT(), ags.get(0).getRIGHTPRE());
					}
				}
				
				if (!TextUtils.isEmpty(ags.get(0).getLEFTHEIGHT())) {
					xueyazuo.setText(ags.get(0).getLEFTHEIGHT() + "/" + ags.get(0).getLEFTPRE() + " mmHg");
					if (!DataCalc.calcXueYa(ags.get(0).getLEFTHEIGHT(), ags.get(0).getLEFTPRE())) {
						jiLunResult = jiLunResult+"左侧测量"
								+ DataCalc.getXueYa(ags.get(0).getLEFTHEIGHT(), ags.get(0).getLEFTPRE());
					}
				}


				tizhizhishu.setText(ags.get(0).getBMI()+"Kg/m²");
				if (!TextUtils.isEmpty(ags.get(0).getTEM())) {
					tiwen.setText(ags.get(0).getTEM()+ " ℃" );
					String tiwenString = ags.get(0).getTEM();
					if (!DataCalc.calcTiWen(tiwenString)) {
						jiLunResult = jiLunResult + DataCalc.getTiWen(tiwenString);
					}
				}

				mailv.setText(ags.get(0).getPULSERATE() + " 次/分");

			}
			if (assistChecks.size() > 0) {
				if (!TextUtils.isEmpty(assistChecks.get(0).getFPGL())) {
					xuetang.setText(assistChecks.get(0).getFPGL() + " mmol/L");
					if (!DataCalc.calcXueTang("0", assistChecks.get(0).getFPGL())) {
						jiLunResult = jiLunResult + DataCalc.getXueTang("0", assistChecks.get(0).getFPGL());
					}
				}
				if (!TextUtils.isEmpty(assistChecks.get(0).getPRO())) {
					PRO.setText(assistChecks.get(0).getPRO());
				}
				if (!TextUtils.isEmpty(assistChecks.get(0).getKET())) {
					KET.setText(assistChecks.get(0).getKET());
				}
				if (!TextUtils.isEmpty(assistChecks.get(0).getGLU())) {
					GLU.setText(assistChecks.get(0).getGLU());
				}
				if (!TextUtils.isEmpty(assistChecks.get(0).getBLD())) {
					BLD.setText(assistChecks.get(0).getBLD());
				}
				
				if(!TextUtils.isEmpty(assistChecks.get(0).getECG())){
					xindian.setText(setChecked(assistChecks.get(0).getECG())+"  "+assistChecks.get(0).getECG_EX());
				}
				
				if(!TextUtils.isEmpty(assistChecks.get(0).getCHESTX())){
					xiongtou.setText(setChecked(assistChecks.get(0).getCHESTX())+"  "+assistChecks.get(0).getCHESTX_EX());
				}
				if(!TextUtils.isEmpty(assistChecks.get(0).getBCHAO() )){
					b_chao.setText(setChecked(assistChecks.get(0).getBCHAO())+"  "+assistChecks.get(0).getBCHAO_EX());
				}
			}
			if (acs.size() > 0) {
				if (acs.get(0).getCHECKDATE() != null) {
					date.setText(formatter.format(acs.get(0).getCHECKDATE()));
				}
			}
			List<SBCL_DATA> sbcl_DATAs = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='血氧'");
			if (sbcl_DATAs.size() > 0) {
				xueyang.setText(sbcl_DATAs.get(sbcl_DATAs.size() - 1).getCL_VALUE());
			}
			if (TextUtils.isEmpty(jiLunResult)) {
				jielun.setText("检查结论：您的测量结果很正常");
			} else {
				jielun.setText("检查结论：" + jiLunResult);
			}
			jianyi.setText("体检建议：\n        1.少吃肉，根据人体的PH来看，人的血液和细胞内都是成弱碱性，而肉类会破坏酸碱平衡，容易诱发疾病。\n"
					+ "        2.多晒太阳，多晒太阳对人生有很多好处，但也要注意晒太阳的时间，注意做好防晒工作，另外冬天晒太阳特别好。\n"
					+ "        3.以步当车，不要一点点路就不愿意走，那样人会慢慢退化掉，适当步行可以缓解久坐的不适，是一种很经济很可行的锻炼方式。\n"
					+ "        4.常常唱歌，唱歌不仅可以释放压力，调节心情，还可以促进面部血液循环，预防多种疾病。\n"
					+ "        5.饭后休息，说的就是饭后由于消化系统需要血液提供能量去消化食物，如果这时大量运动势必会抢走部分血液，对身体不好，小步走倒是可以的。\n"
					+ "        6.挺起胸，走路做事挺起胸来，可以保持好的身形，好的形象气质，还有可以增大肺活量。\n"
					+ "        7.养兴趣，在家养些喜欢的花草，除了可观赏性之外，还可以改善空气质量，陶冶人的情操。");

		}

	}

	private void setListens() {
		btn_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ShowJieLunPicActivity.this.finish();
			}
		});
	}
	/**
	 * 设置显示数据
	 * @param id
	 * @return
	 * @author 王婷婷
	 * @date 2017年7月4日 下午4:12:46
	 */
	public String setChecked(String id) {
		 Log.e(TAG, "tt"+id);
		String result="";
		List<BD_DD_INFO> infos=NovaApplication.fdbmanger.findAllByWhereInInfoDB (BD_DD_INFO.class, "ID='"+id+"'");
		 if(infos.size()>0){
			 result=infos.get(0).getDATAINFO();
			  Log.e(TAG, "tt"+result);
		 }
		 return result;
	}
}
